package com.controller.selling 
{

	import com.model.valueObjects.VoInvoice;
	import com.service.InvoiceHeader;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class InvoiceController extends AbstractController
	{
	
		[Inject]
		public var invoiceDetails:InvoiceHeader;
		
		public function InvoiceController()
		{
			super();
		}
		
		[EventHandler(event="InvoiceEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(invoiceDetails.getAllInvoices(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			if(model.currentMenu=='invoiceBtn')
				model.currentSellTableIndex=3;
			
			model.invoiceList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="InvoiceEvent.GET_INVOICE_LIST",properties="customerFk")]
		public function getDOList(customerFk:Number):void{
			sh.executeServiceCall(invoiceDetails.getInvoiceForCRDB(customerFk),getInvoiceListResult, handleFault );
		}
		private function getInvoiceListResult(event:ResultEvent):void
		{
			model.invoiceHeaderList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="InvoiceEvent.ADD", properties="invoicevo")]
		public function add(invoicevo:VoInvoice):void
		{
			sh.executeServiceCall(invoiceDetails.addNewInvoice(invoicevo), addResult, handleFault );	
		}
		
		private function addResult(event:ResultEvent):void
		{
			var invoicevo:VoInvoice=event.result as VoInvoice;
			model.invoiceList.addItem(invoicevo)
			model.invoiceList.refresh();
			if(model.saveCloseBtn)model.currentSellFormsIndex=0;	
			Alert.show( "Invoice added" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			getAll();
		}
		
		[EventHandler(event="InvoiceEvent.UPDATE", properties="invoicevo")]
		public function update(invoicevo:VoInvoice):void
		{
			sh.executeServiceCall(invoiceDetails.editInvoice(invoicevo), editResult, handleFault );	
		}
		
		private function editResult(event:ResultEvent):void
		{
			getAll();
			model.currentSellFormsIndex=0;	
			Alert.show( "Invoice Updated" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="InvoiceEvent.DELETE", properties="invoicevo")]
		public function deletef(invoicevo:VoInvoice):void
		{
			sh.executeServiceCall(invoiceDetails.deleteInvoice(invoicevo),deleteResult, handleFault );	
		}		
		private function deleteResult(event:ResultEvent):void
		{
			model.currentSellFormsIndex=0;	
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show( "Invoice Deleted");	
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}